<?php
# 创建缓存目录
function mkLogDir($logDir)
{
    $dirPath = APP_PATH . '/runtime/' . $logDir;
    if (!is_dir($dirPath)) {
        mkdir($dirPath, 0755, true);
    }
    return $dirPath;

}
function dump_log($message = null, $logName = '')
{
    $dir = mkLogDir('log/');
    file_put_contents($dir . date('Ymd') . '_' . $logName . '.log', '时间: ' . date('Y-m-d h:i:s') . ' ' . $message . PHP_EOL, FILE_APPEND);
}
// 判断是否为手机端
function isMobile()
{
    $useragent               = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $useragent_commentsblock = preg_match('|\(.*?\)|', $useragent, $matches) > 0 ? $matches[0] : '';
    function CheckSubstrs($substrs, $text)
    {
        foreach ($substrs as $substr) {
            if (false !== strpos($text, $substr)) {
                return true;
            }
        }

        return false;
    }
    $mobile_os_list    = array('Google Wireless Transcoder', 'Windows CE', 'WindowsCE', 'Symbian', 'Android', 'armv6l', 'armv5', 'Mobile', 'CentOS', 'mowser', 'AvantGo', 'Opera Mobi', 'J2ME/MIDP', 'Smartphone', 'Go.Web', 'Palm', 'iPAQ');
    $mobile_token_list = array('Profile/MIDP', 'Configuration/CLDC-', '160×160', '176×220', '240×240', '240×320', '320×240', 'UP.Browser', 'UP.Link', 'SymbianOS', 'PalmOS', 'PocketPC', 'SonyEricsson', 'Nokia', 'BlackBerry', 'Vodafone', 'BenQ', 'Novarra-Vision', 'Iris', 'NetFront', 'HTC_', 'Xda_', 'SAMSUNG-SGH', 'Wapaka', 'DoCoMo', 'iPhone', 'iPod');

    $found_mobile = CheckSubstrs($mobile_os_list, $useragent_commentsblock) ||
    CheckSubstrs($mobile_token_list, $useragent);

    if ($found_mobile) {
        return true;
    }
    return false;
}

// 验证手机号是否正确
function checkMobile($mobile)
{
    if (preg_match("/^1[3-9]\d{9}$/", $mobile)) {
        return true;
    }
    return false;
}
//获取真实ip
function ip()
{
    if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $ip = getenv('HTTP_CLIENT_IP');
    } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $ip = getenv('REMOTE_ADDR');
    } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return preg_match('/[\d\.]{7,15}/', $ip, $matches) ? $matches[0] : '';
}

// 生成密文
function setPassword($password, $salt = '')
{
    return md5(sha1($salt . md5($password . $salt)) . $salt);
}
/**
 * 验证签名
 *
 */
function checkSign($queryarr, $appsecret, $signature)
{
    $str = makeSign($queryarr, $appsecret);
    if ($str == strtolower($signature)) {
        return true;
    } else {
        return false;
    }

}

/**
 * 生成签名
 *
 */
function makeSign($queryarr, $appsecret)
{
    ksort($queryarr, SORT_STRING);
    $str = implode($queryarr);
    $str .= $appsecret;
    //exit($str);
    $str = md5($str);

    return $str;
}

function send_post($url, $post_data)
{
    $postdata = http_build_query($post_data);
    $options  = array(
             'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
    ],
        'http' => array(
            'method'  => 'POST',
            'header'  => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 10, // 超时时间（单位:s）
        ),
    );
    $context = stream_context_create($options);
    $result  = file_get_contents($url, false, $context);
    return $result;
}

